//
//  DetailCell.m
//  lvtu
//
//  Created by Apple on 12-6-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DetailCell.h"
#import "NSDate+Helper.h"

@implementation DetailCell
@synthesize delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        dateDetailView = [[DateDetailView alloc] initWithFrame:CGRectMake(10, 25, 253, 23)];
        dateDetailView.dateLabel.font = [UIFont systemFontOfSize:14];
        dateDetailView.dateLabel.backgroundColor = [UIColor clearColor];
        CGRect frame = dateDetailView.detailLabel.frame;
        dateDetailView.detailLabel.frame = CGRectMake(frame.origin.x+30, frame.origin.y, frame.size.width, frame.size.height);
        dateDetailView.detailLabel.font = [UIFont boldSystemFontOfSize:17];
        dateDetailView.detailLabel.textColor = [UIColor colorWithRed:252/255.0 green:100/255.0 blue:22/255.0 alpha:1];
        dateDetailView.detailLabel.backgroundColor = [UIColor clearColor];
//        [self addSubview:dateDetailView];
        
        //    dataPartView1.delegate = self;
        dataPartView1 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 0, 290, 50)];
        [dataPartView1 setButtonStyle:NONEBUTTON];
        dataPartView1.elementType = 1;
        [self addSubview:dataPartView1];
        
        //    dataPartView2.delegate = self;
        dataPartView2 = [[DataPartView alloc] initWithFrame:CGRectMake(0, dataPartView1.frame.size.height, 290, 50)];
        [dataPartView2 setButtonStyle:NONEBUTTON];
        dataPartView2.elementType = 2;
        [self addSubview:dataPartView2];
        
        //    dataPartView3.delegate = self;
        dataPartView3 = [[DataPartView alloc] initWithFrame:CGRectMake(0, dataPartView1.frame.size.height+dataPartView2.frame.size.height, 290, 50)];
        [dataPartView3 setButtonStyle:NONEBUTTON];
        dataPartView3.elementType = 3;
        [self addSubview:dataPartView3];
    }
    return self;
}

- (void)dealloc
{
    [dateDetailView release];
    [dataPartView1 release];
    [dataPartView2 release];
    [dataPartView3 release];
    [super dealloc];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

static NSInteger DateSort(id s1, id s2, void *context)
{
    NSDate* date1 = [NSDate dateWithDateString:(NSString*)s1];
    NSTimeInterval time1 = [date1 timeIntervalSinceReferenceDate];
    NSDate* date2 = [NSDate dateWithDateString:(NSString*)s2];
    NSTimeInterval time2 = [date2 timeIntervalSinceReferenceDate];
    
    if (time1 < time2)
        return NSOrderedDescending;
    else if (time1 > time2)
        return NSOrderedAscending;
    else
        return NSOrderedSame;
}
- (void)setCell:(NSDictionary*)dict Index:(NSInteger)index Type:(BOOL)isEdit
{
    dateDetailView.dateLabel.text = [NSString stringWithFormat:@"第%d天",index+1];
    NSArray* dictArray = [dict allKeys];
    NSArray* sortArray = [dictArray sortedArrayUsingFunction:DateSort context:NULL];
    dateDetailView.detailLabel.text = [sortArray objectAtIndex:[sortArray count]-index-1];
    dateIndex = index;
    
    NSDictionary* datePartDict = [dict objectForKey:dateDetailView.detailLabel.text];
    NSDictionary* eventDict = [datePartDict objectForKey:@"1"];
    if ([eventDict count] > 0) {
        [dataPartView1 setFrame:CGRectMake(0, 0, 290, 160)];
        [dataPartView1 setEventShow:[eventDict objectForKey:@"list"]];
    }
    else
    {
        [dataPartView1 setFrame:CGRectMake(0, 0, 290, 50)];
    }
    if (isEdit) {
        [dataPartView1 setEventCountForEdit:[eventDict count]];
        dataPartView1.delegate = self;
    }
    dataPartView1.partLabel.text = @"上午： ";
    if ([eventDict count] == 0 || ![eventDict objectForKey:@"city"] || [[eventDict objectForKey:@"city"] isEqualToString:@"(null)"]) {
        dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%d事件",[[eventDict objectForKey:@"list"] count]];
    }
    else {
        dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%d事件,%@",[[eventDict objectForKey:@"list"] count],[eventDict objectForKey:@"city"]];
    }
    
    eventDict = [datePartDict objectForKey:@"2"];
    if ([eventDict count] > 0) {
        [dataPartView2 setFrame:CGRectMake(0, dataPartView1.frame.size.height, 290, 160)];
        [dataPartView2 setEventShow:[eventDict objectForKey:@"list"]];
    }
    else
    {
        [dataPartView2 setFrame:CGRectMake(0, dataPartView1.frame.size.height, 290, 50)];
    }
    if (isEdit) {
        [dataPartView2 setEventCountForEdit:[eventDict count]];
        dataPartView2.delegate = self;
    }
    dataPartView2.partLabel.text = @"下午： ";
    if ([eventDict count] == 0 ||[[eventDict objectForKey:@"city"] isEqualToString:@"(null)"]) {
        dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%d事件",[[eventDict objectForKey:@"list"] count]];
    }
    else {
        dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%d事件,%@",[[eventDict objectForKey:@"list"] count],[eventDict objectForKey:@"city"]];
    }
    
    eventDict = [datePartDict objectForKey:@"3"];
    if ([eventDict count] > 0) {
        [dataPartView3 setFrame:CGRectMake(0, dataPartView1.frame.size.height+dataPartView2.frame.size.height, 290, 160)];
        [dataPartView3 setEventShow:[eventDict objectForKey:@"list"]];
    }
    else{
        [dataPartView3 setFrame:CGRectMake(0, dataPartView1.frame.size.height+dataPartView2.frame.size.height, 290, 50)];
    }
    if (isEdit) {
        [dataPartView3 setEventCountForEdit:[eventDict count]];
        dataPartView3.delegate = self;
    }
    dataPartView3.partLabel.text = @"晚上： ";
    if ([eventDict count] == 0 ||[[eventDict objectForKey:@"city"] isEqualToString:@"(null)"]) {
        dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%d事件",[[eventDict objectForKey:@"list"] count]];
    }
    else {
        dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%d事件,%@",[[eventDict objectForKey:@"list"] count],[eventDict objectForKey:@"city"]];
    }
}

- (void)touchUpInsideEditViewCell:(NSString*)IndexText button:(NSInteger)buttontype elementType:(ElementType)type
{
    [delegate touchUpInsideDetailCell:dateDetailView.dateLabel.text Index:dateIndex Detail:[NSString stringWithFormat:@"%@，%@",dateDetailView.detailLabel.text,IndexText] button:buttontype ementType:type];
}

@end
